# 线程: 无序
print("---------------- 线程之间共享资源 ---------------------")
# 线程之间共享资源(全局变量)
from threading import  Thread
import time
li = [] # 定义全局变量
# 写入数据
def write_data():
    for i in range(5):
        li.append(i)
        time.sleep(0.01)
    print("写入的数据是:", li)

def read_data():
    print("读取的数据是", li)

if __name__ == "__main__":
    # 创建子线程
    t1 = Thread(target=write_data)
    t2 = Thread(target=read_data)

    t1.start()
    t1.join()  # 阻塞线程 等待 t1 任务执行结束
    t2.start()
    t2.join()


print("---------------- 线程之间资源竞争 ---------------------")
a = 0
b = 1000000
# 循环次就给全局变量 a+1

def plus():
    for i in range(b):
        global a # 局部变量不能修改全局变量
        a += 1
    print("plus :", a)

def increase():
    for i in range(b):
        global a # 局部变量不能修改全局变量
        a += 1
    print("increase :", a)


if __name__ == "__main__":
    p1= Thread(target=plus)
    p2 = Thread(target=increase)
    p1.start()
    p1.join()
    p2.start()